import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/buyer_entity.dart';
import 'package:library_base/entities/goods_child_entity.dart';
import 'package:library_base/entities/shipper_entity.dart';
import 'package:library_base/entities/suppliers_entity.dart';
import 'package:library_base/entities/tasks_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/dialog/loading_center_dialog.dart';
import 'package:library_base/widget/image/local_image.dart';

class GenerateWidgetUtil {
  ///页面左右间隔
  static EdgeInsetsGeometry tipVGap() {
    return EdgeInsets.symmetric(vertical: 24.w);
  }

  ///页面左右间隔
  static EdgeInsetsGeometry tipGapOnlyLeft() {
    return EdgeInsets.only(left: 24.w);
  }

  ///页面左右间隔
  static EdgeInsetsGeometry pageHGap() {
    return EdgeInsets.symmetric(horizontal: 24.w);
  }

  static EdgeInsetsGeometry buttonGap() {
    return EdgeInsets.only(right: 24.w, left: 24.w, top: 21.w, bottom: 68.w);
  }

  static EdgeInsetsGeometry vw40Gap() {
    return EdgeInsets.symmetric(vertical: 40.w);
  }

  static EdgeInsetsGeometry vw25Gap() {
    return EdgeInsets.symmetric(vertical: 25.w);
  }

  static EdgeInsetsGeometry hw30Gap() {
    return EdgeInsets.symmetric(vertical: 30.w);
  }

  ///设置页头像上下的间距
  static EdgeInsetsGeometry avatarHGap() {
    return EdgeInsets.symmetric(horizontal: 24.w, vertical: 40.w);
  }

  ///设置页头像上下的间距
  static EdgeInsetsGeometry optionSetUpGap() {
    return EdgeInsets.symmetric(horizontal: 24.w, vertical: 36.w);
  }

  ///设置页头像上下的间距
  static EdgeInsetsGeometry optionSetUpGap1() {
    return EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.w);
  }

  ///页面左右间隔
  static EdgeInsetsGeometry top14HGap() {
    return EdgeInsets.only(top: 14.w);
  }


  static LoadingCenterDialog loadingDialog(BuildContext context, {String? content}) {
    return LoadingCenterDialog(
      context,
      content: content ?? MultiLanguage.loading.tr,
      backgroundColor: ThemeUtil.getTheme().colorScheme.secondary,
      textColor: ThemeUtil.getTheme().normalTextColor(),
    );
  }

  static Widget getSusItem(
    BuildContext context,
    String tag, {
    bool isHasBottomLine = false,
  }) {
    if (tag == '★') {
      tag = '★ 热门城市';
    }
    return Container(
      padding: EdgeInsets.all(24.w),
      width: MediaQuery.of(context).size.width,
      color: ThemeUtil.getTheme().secondaryBgColor(),
      alignment: Alignment.centerLeft,
      child: Text(
        tag,
        softWrap: false,
        style: TextStyles.textNormal30,
      ),
    );
  }

  static Widget getListItem(
    BuildContext context,
    SuppliersEntity model, {
    bool isHasBottomLine = false,
    Callback<SuppliersEntity>? onTap,
  }) {
    List<Widget> widgets = [];

    List<Widget> item = [];
    item.add(Expanded(child: Text(model.name, style: TextStyles.textNormal30)));
    if (model.isSelect) {
      item.add(Padding(
          padding: EdgeInsets.only(right: 40.w, left: 8.w),
          child: LocalImage('admin_√', package: Constant.packAdmin(), width: 25.r, height: 25.r)));
    }
    widgets.add(Container(
        padding: vw40Gap(),
        alignment: Alignment.centerLeft,
        margin: pageHGap(),
        child: Row(
          children: item,
        )));
    if (isHasBottomLine) {
      widgets.add(Container(
        margin: pageHGap(),
        color: ThemeUtil.getTheme().dividingLineColor(),
        width: double.infinity,
        height: 0.5,
      ));
    }
    return columnInkBgWell<SuppliersEntity>(model, children: widgets, onTap: onTap);
  }

  static Widget getWarehouseListItem(
    BuildContext context,
    WarehouseEntity model, {
    bool isHasBottomLine = false,
    Callback<WarehouseEntity>? onTap,
  }) {
    List<Widget> widgets = [];

    List<Widget> item = [];
    item.add(Expanded(child: Text(model.whName, style: TextStyles.textNormal30)));
    if (model.isSelect) {
      item.add(Padding(
          padding: EdgeInsets.only(right: 40.w, left: 8.w),
          child: LocalImage('admin_√', package: Constant.packAdmin(), width: 25.r, height: 25.r)));
    }
    widgets.add(Container(
        padding: vw40Gap(),
        alignment: Alignment.centerLeft,
        margin: pageHGap(),
        child: Row(
          children: item,
        )));
    if (isHasBottomLine) {
      widgets.add(Container(
        margin: pageHGap(),
        color: ThemeUtil.getTheme().dividingLineColor(),
        width: double.infinity,
        height: 0.5,
      ));
    }
    return columnInkBgWell<WarehouseEntity>(model, children: widgets, onTap: onTap);
  }

  static Widget getShipperListItem(
    BuildContext context,
    ShipperEntity model, {
    bool isHasBottomLine = false,
    Callback<ShipperEntity>? onTap,
  }) {
    List<Widget> widgets = [];

    List<Widget> item = [];
    item.add(Expanded(child: Text(model.shipperName, style: TextStyles.textNormal30)));
    if (model.isSelect) {
      item.add(Padding(
          padding: EdgeInsets.only(right: 40.w, left: 8.w),
          child: LocalImage('admin_√', package: Constant.packAdmin(), width: 25.r, height: 25.r)));
    }
    widgets.add(Container(
        padding: vw40Gap(),
        alignment: Alignment.centerLeft,
        margin: pageHGap(),
        child: Row(
          children: item,
        )));
    if (isHasBottomLine) {
      widgets.add(Container(
        margin: pageHGap(),
        color: ThemeUtil.getTheme().dividingLineColor(),
        width: double.infinity,
        height: 0.5,
      ));
    }
    return columnInkBgWell<ShipperEntity>(model, children: widgets, onTap: onTap);
  }

  static Widget getGoodsItem(
    GoodsChildEntity model, {
    bool isHasBottomLine = true,
    Callback<GoodsChildEntity>? onTap,
  }) {
    List<Widget> widgets = [];

    List<Widget> item = [];

    item.add(Padding(
        padding: EdgeInsets.only(right: 16.w),
        child: LocalImage(model.isSelect ? 'admin_checked' : 'admin_no_checked',
            package: Constant.packAdmin(), width: 34.r, height: 34.r)));

    item.add(Expanded(child: Text(model.goodsName, style: TextStyles.textNormal30)));

    widgets.add(Container(
        padding: vw40Gap(),
        alignment: Alignment.centerLeft,
        margin: pageHGap(),
        child: Row(
          children: item,
        )));
    if (isHasBottomLine) {
      widgets.add(Container(
        margin: pageHGap(),
        color: ThemeUtil.getTheme().dividingLineColor(),
        width: double.infinity,
        height: 0.5,
      ));
    }
    return columnInkBgWell<GoodsChildEntity>(model, children: widgets, onTap: onTap);
  }

  static Widget getBuyerListItem(
    BuyerEntity model, {
    bool isHasBottomLine = false,
    Callback<BuyerEntity>? onTap,
  }) {
    List<Widget> widgets = [];

    List<Widget> item = [];

    item.add(Expanded(child: Text(model.nickname, style: TextStyles.textNormal30)));
    if (model.isSelect) {
      item.add(Padding(
          padding: EdgeInsets.only(right: 40.w, left: 8.w),
          child: LocalImage('admin_√', package: Constant.packAdmin(), width: 25.r, height: 25.r)));
    }
    widgets.add(Container(
        padding: vw40Gap(),
        alignment: Alignment.centerLeft,
        margin: pageHGap(),
        child: Row(
          children: item,
        )));
    if (isHasBottomLine) {
      widgets.add(Container(
        margin: pageHGap(),
        color: ThemeUtil.getTheme().dividingLineColor(),
        width: double.infinity,
        height: 0.5,
      ));
    }
    return columnInkBgWell<BuyerEntity>(model, children: widgets, onTap: onTap);
  }

  static Widget inputInfo({
    BorderRadiusGeometry borderRadius = const BorderRadius.all(Radius.circular(6.0)),
    TextEditingController? controller,
    TextInputType? keyboardType = TextInputType.number,
    String unit = '',
    String? hintText,
    bool enabled = true,
    Color? color,
    double? width = double.infinity,
    double? height,
    FocusNode? focusNode,
    List<TextInputFormatter>? inputFormatters,
    ValueChanged<String>? onChanged,
  }) {
    return GestureDetector(
      onTap: () {
        focusNode?.requestFocus();
      },
      child: Container(
          alignment: Alignment.center,
          width: width,
          height: height,
          decoration: BoxDecoration(
            color: color ?? ThemeUtil.getTheme().secondaryBgColor(),
            borderRadius: borderRadius,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 24.w),
                  alignment: Alignment.center,
                  // width: double.infinity,
                  child: TextField(
                    focusNode: focusNode,
                    style: enabled ? normalTextColor28 : TextStyles.textSixthColor28,
                    enabled: enabled,
                    inputFormatters: inputFormatters,
                    onChanged: onChanged,
                    autofocus: false,
                    controller: controller,
                    keyboardType: keyboardType,
                    textAlignVertical: TextAlignVertical.center,
                    decoration: InputDecoration(
                      //下面border都是为了移除内置的padding
                      focusedBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      disabledBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      enabledBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                      border: InputBorder.none,
                      //这个属性是为了移除TextField额外的高度
                      isDense: true,
                      labelStyle: normalTextColor28,
                      hintText: hintText,
                      hintStyle: TextStyles.textSixthColor28,
                    ),
                  ),
                ),
              ),
              Visibility(
                visible: unit.isNotEmpty,
                child: Container(
                  color: ThemeUtil.getTheme().dividingLineColor(),
                  width: 2.w,
                  height: 50.w,
                ),
              ),
              Visibility(
                visible: unit.isNotEmpty,
                child: Container(
                    alignment: Alignment.center,
                    width: 120.w,
                    child: Text(
                      unit,
                      style: TextStyles.textSixthColor28,
                    )),
              ),
            ],
          )),
    );
  }

  static Widget inputInfo1({
    BorderRadiusGeometry borderRadius = const BorderRadius.all(Radius.circular(6.0)),
    TextEditingController? controller,
    TextInputType? keyboardType = TextInputType.number,
    String? hintText,
    bool enabled = true,
    Color? color,
    double? width = double.infinity,
    double? height,
    FocusNode? focusNode,
    ValueChanged<String>? onChanged,
  }) {
    return GestureDetector(
      onTap: () {
        focusNode?.requestFocus();
      },
      child: Container(
          alignment: Alignment.center,
          width: width,
          height: height,
          decoration: BoxDecoration(
            color: color ?? ThemeUtil.getTheme().secondaryBgColor(),
            borderRadius: borderRadius,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 24.w),
                  alignment: Alignment.center,
                  // width: double.infinity,
                  child: TextField(
                    style: enabled ? normalTextColor28 : TextStyles.textSixthColor28,
                    enabled: enabled,
                    onChanged: onChanged,
                    autofocus: false,
                    focusNode: focusNode,
                    controller: controller,
                    keyboardType: keyboardType,
                    textAlignVertical: TextAlignVertical.center,
                    decoration: InputDecoration(
                      //下面border都是为了移除内置的padding
                      focusedBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      disabledBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      enabledBorder: const OutlineInputBorder(
                          borderSide: BorderSide(width: 0, color: Colors.transparent)),
                      contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                      border: InputBorder.none,
                      //这个属性是为了移除TextField额外的高度
                      isDense: true,
                      labelStyle: normalTextColor28,
                      hintText: hintText,
                      hintStyle: TextStyles.textSixthColor28,
                    ),
                  ),
                ),
              ),
            ],
          )),
    );
  }

  static Widget roundWidget({
    Widget? child,
    double? width,
    double? height,
    Clip clipBehavior = Clip.none,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    AlignmentGeometry? alignment = Alignment.center,
    Decoration? decoration,
  }) {
    return Container(
        clipBehavior: clipBehavior,
        margin: margin,
        padding: padding,
        alignment: alignment,
        width: width,
        height: height,
        decoration: decoration,
        child: child);
  }

  static Widget shapeNetImage(
    String imageUrl, {
    double width = 50,
    double height = 50,
    BoxConstraints? constraints,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    Decoration? decoration,
    Clip clipBehavior = Clip.none,
    AlignmentGeometry? alignment = Alignment.center,
    LoadingErrorWidgetBuilder? errorWidget,
    PlaceholderWidgetBuilder? placeholder,
  }) {
    return Container(
      constraints: constraints,
      margin: margin,
      width: width,
      height: height,
      decoration: decoration,
      clipBehavior: clipBehavior,
      child: CachedNetworkImage(
        imageUrl: imageUrl,
        placeholder: placeholder ??
            (context, url) {
              return Container(
                color: ThemeUtil.getTheme().secondaryBgColor(),
                padding: EdgeInsets.symmetric(horizontal: width * 0.3, vertical: height * 0.3),
                child: SpinKitCircle(
                    color: ThemeUtil.getTheme().normalTextColor(),
                    size: width > height ? height * 0.3 : width * 0.3),
              );
            },
        errorWidget: errorWidget ??
            (context, url, error) {
              return Container(
                child: LocalImage('base_placeholder',
                    package: Constant.packLib(), width: 25.r, height: 25.r),
              );
            },
      ),
    );
  }

  static Widget taskItemRoundNetImage(
    String imageUrl, {
    EdgeInsetsGeometry? margin,
    Decoration? decoration,
    Clip clipBehavior = Clip.hardEdge,
    AlignmentGeometry? alignmehnt = Alignment.center,
    LoadingErrorWidgetBuilder? errorWidget,
    PlaceholderWidgetBuilder? placeholder,
  }) {
    return shapeNetImage(
      imageUrl,
      width: 169.w,
      height: 169.w,
      clipBehavior: clipBehavior,
      margin: margin,
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
    );
  }

  static Widget goodItemRoundNetImage(
    String imageUrl, {
    Decoration? decoration,
    Clip clipBehavior = Clip.hardEdge,
    AlignmentGeometry? alignment = Alignment.center,
    LoadingErrorWidgetBuilder? errorWidget,
    PlaceholderWidgetBuilder? placeholder,
  }) {
    return shapeNetImage(
      imageUrl,
      width: 126.w,
      height: 126.w,
      clipBehavior: clipBehavior,
      margin: EdgeInsets.all(24.w),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
    );
  }

  static Widget roundOrangeWidget({
    Color? color,
    Widget? child,
    double? width,
  }) {
    return roundWidget(
        width: width,
        decoration: BoxDecoration(
          color: color ?? ThemeUtil.getTheme().fourthTextColor(),
          borderRadius: const BorderRadius.only(
              topRight: Radius.circular(6.0), bottomRight: Radius.circular(6.0)),
        ),
        child: child);
  }

  static Widget buildTaskItem(
    TasksEntity model, {
    Callback<TasksEntity>? onTap,
    Callback<TasksEntity>? onItemClick,
  }) {
    List<Widget> widgets = [];

    widgets.add(gapH24);

    widgets.add(GestureDetector(
      onTap: () {
        onTap?.call(model);
      },
      child: Container(
        color: ThemeUtil.getTheme().colorScheme.secondary,
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        child: Row(
          children: [
            Padding(
                padding: EdgeInsets.only(right: 24.w),
                child: LocalImage(model.isSelect ? 'admin_checked' : 'admin_no_checked',
                    package: Constant.packAdmin(), width: 34.r, height: 34.r)),
            Expanded(child: Text(model.purchaseOrderNumber, style: normalTextColor28))
          ],
        ),
      ),
    ));
    widgets.add(gapH24);
    widgets.add(Container(
      color: ThemeUtil.getTheme().dividingLineColor(),
      width: double.infinity,
      height: 0.5,
    ));
    widgets.add(gapH18);
    widgets.add(Padding(
        padding: pageHGap(),
        child: Text("${MultiLanguage.supplierNew.tr} : ${model.supplierName}",
            style: normalTextColor28)));
    widgets.add(gapH18);
    widgets.add(Padding(
        padding: pageHGap(),
        child: Text("${MultiLanguage.commodityQuantity.tr} : ${NumUtil.formartNum(model.goodsCount)}",
            style: normalTextColor28)));
    widgets.add(gapH18);
    widgets.add(Padding(
        padding: pageHGap(),
        child: Text("${MultiLanguage.creationTime.tr} : ${model.createTime}",
            style: normalTextColor28)));

    widgets.add(gapH18);
    widgets.add(Padding(
        padding: pageHGap(),
        child: Text("${MultiLanguage.remark.tr} : ${model.remark}",
            style: normalTextColor28)));

    widgets.add(gapH24);
    widgets.add(Container(
      margin: pageHGap(),
      color: ThemeUtil.getTheme().dividingLineColor(),
      width: double.infinity,
      height: 0.5,
    ));

    return GestureDetector(
      onTap: () {
        onItemClick?.call(model);
      },
      child: roundWidget(
        margin: pageHGap(),
        decoration: DecorationRes.rs8,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: widgets,
        ),
      ),
    );
  }

  // static const String deliveryTime = "deliveryTime";
  // static const String shortName = "shortName";

  static Widget inputRichText(String data,{String prefix = "* "}) {
    return RichText(
      text: TextSpan(
          text: prefix,
          style: TextStyles.textEighthColor28,
          children: [TextSpan(text: '$data：', style: normalTextColor28)]),
    );
  }
}
